package com.jn.retrofit.views;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.jn.retrofit.R;
import com.jn.retrofit.adapter.HotSearchAdapter;
import com.jn.retrofit.entity.HotSearchResponse;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ResultActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private HotSearchAdapter adapter;
    private List<HotSearchResponse.HotSearchItem> hotSearchItems = new ArrayList<>();
    private Button backButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        recyclerView = findViewById(R.id.recycler_view);
        backButton = findViewById(R.id.button_back);

        adapter = new HotSearchAdapter(hotSearchItems, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        String jsonData = getIntent().getStringExtra("hotSearchItems");
        if (jsonData != null) {
            Type listType = new TypeToken<List<HotSearchResponse.HotSearchItem>>() {}.getType();
            hotSearchItems = new Gson().fromJson(jsonData, listType);
            adapter.updateData(hotSearchItems);
        } else {
            Toast.makeText(this, "未获取到数据", Toast.LENGTH_LONG).show();
        }

        backButton.setOnClickListener(view -> finish());
    }
}
